//
//  ZHNetworking.m
//  LT
//
//  Created by dllo on 16/5/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ZHNetworking.h"
#import "Reachability.h"
#import "AFNetworking.h"
@implementation ZHNetworking

+(void)zhGETWithURL:(NSString *)url parameter:(NSDictionary *)dic succes:(succesBlock)sucBlock failure:(failureBlock)errorBlock{
//逻辑
    //1.先从本地取数据,显示在页面上
    //2.再从网络获取数据
    //2.1如果有望,显示最新数据
    //2.2如果没有网,提示用户"网络不给力"

    if ([self checkNetworkingStatues]) {
       AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //设置请求类型
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
        [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            //回调
            sucBlock(responseObject);
            
            //更新本地数据
            
            
            
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            //回调失败的结果
            errorBlock(error);
            
            //从本地数据库取出数据
            
            
            
        }];
        
    }else {
        
        //没有网络,从本地取出数据
        
    }
    
  
}


//检查网络
+(BOOL)checkNetworkingStatues
{
    //创建对象
    //reachabilityForInternetConnection 会自动获取网络状态,并将信息存入对象中
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    if (reachability.currentReachabilityStatus == NotReachable) {
        //无网络
        return NO;
    }else{
        return YES;
    }
    
}


@end
